package com.supermap.tool;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;

/**
 * 
* @ClassName: CORSFilter 
* @Description: 拦截器
* 1.处理调用日志
* 2.处理缓存
* @author yuyao
* @date 2017年12月27日 上午9:02:06
 */
@Component
public class CORSFilter implements Filter {

	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		 /* HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
		  System.out.println("请求接口名称："+httpServletRequest.getRequestURI()); //请求的接口
		  System.out.println("请求方式："+httpServletRequest.getMethod());//请求方式
		  System.out.println("请求IP："+getIpAddress(httpServletRequest));//请求IP
		  
		  //获取请求参数
		   Enumeration<String> names = httpServletRequest.getParameterNames();
		   while (names.hasMoreElements()) {
			  String name = (String) names.nextElement();
			  String value = httpServletRequest.getParameter(name);
			  System.out.println("请求参数名称:"+ name + " 请求参数值:" + value);
	        }*/
	        /*filterChain.doFilter(servletRequest, servletResponse);
	        HttpServletResponse response = (HttpServletResponse) servletResponse;  
	        
	        HttpServletRequest reqs = (HttpServletRequest) servletRequest;  
	  
	        response.setHeader("Access-Control-Allow-Origin",reqs.getHeader("Origin"));  
	        response.setHeader("Access-Control-Allow-Credentials", "true");  
	        response.setHeader("Access-Control-Allow-Headers","x-requested-with,Cache-Control,Pragma,Content-Type,Token");  
	        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	        response.setHeader("Access-Control-Max-Age", "3600");   */
	        
	        
	        filterChain.doFilter(servletRequest,servletResponse); 
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
	
	/** 
     * 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址; 
     *  
     * @param request 
     * @return 
     * @throws IOException 
     */  
    public String getIpAddress(HttpServletRequest request) throws IOException {  
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址  
        String ip = request.getHeader("X-Forwarded-For");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
                ip = request.getHeader("Proxy-Client-IP");  
            }  
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
                ip = request.getHeader("WL-Proxy-Client-IP");  
            }  
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
                ip = request.getHeader("HTTP_CLIENT_IP");  
            }  
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
            }  
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
                ip = request.getRemoteAddr();  
            }  
        } else if (ip.length() > 15) {  
            String[] ips = ip.split(",");  
            for (int index = 0; index < ips.length; index++) {  
                String strIp = (String) ips[index];  
                if (!("unknown".equalsIgnoreCase(strIp))) {  
                    ip = strIp;  
                    break;  
                }  
            }  
        }  
        return ip;  
    }  

}
